import Vue from 'vue'
import VueRouter from 'vue-router'
import register from '@/views/register.vue'
import Login from '@/views/Login.vue'
import userInfo from '@/views/userInfo.vue'
import EtidUser from '@/views/EtidUser.vue'
import index from '../views/index.vue'
import ArticleSp from  '../views/ArticleSp.vue'
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    component: index,
    meta:{
      keepalive:true
    }
  },
  {
    path:'/index',
    component: index,
    meta:{
      keepalive:true
    }
  },
  {
    path: '/register',
    component: register
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/userinfo',
    component: userInfo,
    meta: {
      isToken: true
    }
  },
  {
    path: '/etiduser',
    component: EtidUser,
    meta: {
      isToken: true
    }
  },
  {
    path:'/ArticleSp/:id',
    component:ArticleSp
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (!localStorage.getItem('id') && !localStorage.getItem('token') && to.meta.isToken == true) {
    router.push('/login')
    Vue.prototype.$toast({
      message: '请重新登入'
    });
    return;
  }

  next()
})

export default router
